package com.smartinput.device.domain.model

import com.mensanyun.kotool.domain.model.BaseAbleDomainModel
import jakarta.persistence.Entity
import java.time.LocalDateTime

@Entity
class Device(
    uid: String,
    val deviceId: String,
    var userId: String?=null, // 允许为null，表示匿名设备
    val deviceName: String?,
    val deviceType: String,
    var online: Boolean = false,
    var socketId: String? = null,
    var lastHeartbeat: LocalDateTime? = null,
    enabled: Boolean,
    createdAt: LocalDateTime
) : BaseAbleDomainModel(uid, enabled, createdAt) {
    fun updateStatus(online: Boolean, socketId: String?) {
        this.online = online
        this.socketId = socketId
    }

    fun bindUser(userId: String) {
        this.userId = userId
    }

    fun updateHeartbeat() {
        this.lastHeartbeat = LocalDateTime.now()
    }

    fun validateOwnership(userId: String): Boolean {
        return this.userId == userId
    }

    fun isAnonymous(): Boolean {
        return this.userId == null
    }

    fun canBeClaimedBy(userId: String): Boolean {
        return this.userId == null || this.userId == userId
    }

}